<?php

namespace App\Http\Controllers\Activity;

use App\Contracts\Services\Storage\StorageService;
use App\Entities\Activity;
use App\Entities\ActivityInfo;
use App\Entities\DrawRecord;
use Illuminate\Container\Container;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;

class ApiController
{
    public function winnings(Request $request, $id)
    {
        $uid = $request->get('uid', $request->get('open_id'));
        $perPage = $request->get('per_page', 15);
        $tenant_id = $request->get('tenant_id', '');
        $uniqueid = md5(strtolower($tenant_id.$uid));

        $query = DrawRecord::with([
            'prize' => function ($query) {
                return $query->select([
                    'id',
                    'name',
                    'type_code',
                    'background_image',
                    'exchange_url',
                    'exchange_method',
                    'extends',
                    'expired_start'
                    ,'expired_end'
                ]);
            },
            'prize.sponsors' => function ($query) {
                return $query->select([
                    'prize_id',
                    'name',
                    'logo',
                ]);
            },
            'activity' => function ($query) {
                return $query->select('id', 'name');
            },
            'logistics',
            'logistics.company' => function ($query) {
                return $query->select(['id', 'name', 'bukrs']);
            },
        ]);

        if($id != 'all'){
            $query->where('activity_id', $id);
        }

        if ($uid) {
            $query->where('openid', $uniqueid);
        }

        $records = $query->paginate($perPage, [
            'id',
            'activity_id',
            'prize_id',
            'name',
            'mobile',
            'address',
            'amount',
            'coupon',
            'is_exchange',
            'is_confirm',
        ]);

        $storage = Container::getInstance()->make(StorageService::class);

        foreach ($records as &$record) {
            if ($record->prize) {
                $record->prize->background_image = $storage->toHttpUrl(
                    $record->prize->background_image
                );
                $record->prize->exexpired_start_date = date("Y-m-d",$record->prize->exexpired_start);
                $record->prize->exexpired_end_date = date("m-d",$record->prize->exexpired_end);
                if ($record->prize->extends) {
                    $record->prize->fileds = \GuzzleHttp\json_decode($record->prize->extends, true);
                } else {
                    $record->prize->fileds = [];
                }

                $record->prize->sponsor = $this->randomSponsors($record->prize->sponsors, $storage);
                unset($record->prize->sponsors);
            }
        }
    
        return response()->success($records);
    }

    protected function randomSponsors(Collection $sponsors = null, $storage)
    {
        if (null === $sponsors) {
            return null;
        }

        // $take = $sponsors->random(1);
        $takes = $sponsors->toArray();
        $counts = count($takes);

        if (!$counts) {
            return null;
        }

        $take = $takes[mt_rand(0, $counts - 1)];

        return [
            'logo' => $storage->toHttpUrl($take['logo']),
            'name' => $take['name'],
        ];
    }
}
